package com.sunfeng.chap2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author : 孙峰
 * @Description: read 方法是从通道的当前位置开始读取的 从position 开始读取数据
 * @Date : 2021/10/8  15:17
 */
public class Test2_2 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(new File("d:\\b.txt"));
        FileChannel fileChannel = fileInputStream.getChannel();
        fileChannel.position(2);
        ByteBuffer byteBuffer = ByteBuffer.allocate(5);

        fileChannel.read(byteBuffer);

        byte[] array = byteBuffer.array();

        for (int i = 0; i < array.length; i++) {
            System.out.println((char) array[i]);
        }
        fileChannel.close();
        fileInputStream.close();

    }
}
